import 'package:flutter/material.dart';

import '../../utils/tools/Tools.dart';

class DateTimes {
  /// 选择时间区间
  /// * [context] BuildContext|上下文|必填
  /// * [firstDate] DateTime|开始时间|选填
  /// * [callBack] Function|回调函数|选填
  static showDateRangePickers(
    BuildContext context, {
    Function callBack,
    DateTime firstDate,
    DateTime lastDate,
  }) async {
    return await showDateRangePicker(
      context: context,
      firstDate: firstDate ?? DateTime.now(),
      lastDate: lastDate ?? DateTime(2100),
      initialDateRange: DateTimeRange(
        start: DateTime.now(),
        end: DateTime.now(),
      ),
    ).then((DateTimeRange value) {
      if (value != null) {
        callBack(DateTimeRangeResult.fromJson(
          {"start": value.start, "end": value.end},
        ));
      }
    });
  }
}

/// 选取的区间时间
/// * [start] String|开始时间
/// * [end] String|结束时间
class DateTimeRangeResult {
  String start;
  String end;

  DateTimeRangeResult({this.start, this.end});

  DateTimeRangeResult.fromJson(Map<String, dynamic> json) {
    start = Tools.dateFormat(json['start'], DataFormats.y_mo_d);
    end = Tools.dateFormat(json['end'], DataFormats.y_mo_d);
  }
}
